package com.zx.map.dao.impl;

import com.zx.map.dao.MerchantInfoDao;
import com.zx.map.dao.mapper.MerchantCertificateInfoMapper;
import com.zx.map.dao.mapper.MerchantInfoMapper;
import com.zx.map.dao.mapper.MerchantTerminalInfoMapper;
import com.zx.map.dao.model.*;
import com.zx.platform.dao.mapper.SequenceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ������
 * Date: 15-5-12
 * Time: ����14:12
 * ������Ϣ�����ݲ���-ʵ��
 */
@Component("MerchantInfoDao")
public class MerchantInfoImpl implements MerchantInfoDao {

    @Autowired
    private MerchantInfoMapper mapper;
    
    @Autowired
    private MerchantTerminalInfoMapper merchantTerminalInfoMapper;
    
    @Autowired
    private MerchantCertificateInfoMapper merchantCertificateInfoMapper;
    
    @Autowired
    private SequenceMapper sequencemapper;

    /**
     * 新增
     */
    public String add(MerchantInfo merchantInfo) throws DataAccessException {
    	//设置Id
    	merchantInfo.setMerchantId("M"+sequencemapper.selectByStrAndNextval("agent"));
        int column = mapper.insert(merchantInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return merchantInfo.getMerchantId();
    }

    /**
     * 修改
     * @param merchantInfo
     * @return
     * @throws DataAccessException
     */
    public String update(MerchantInfo merchantInfo) throws DataAccessException {
        int column = mapper.updateByPrimaryKeySelective(merchantInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return merchantInfo.getMerchantId();
    }

    /**
     * 删除
     */
    public void delete(MerchantInfo merchantInfo) throws DataAccessException {
        int column = mapper.deleteByPrimaryKey(merchantInfo.getMerchantId());
        if (column != 1) {
            throw new RuntimeException("");
        }
    }

    /**
     * 分页查找
     */
	public List<MerchantInfo> getPageByExample(int pageSize, int pageBegin,
			MerchantInfoExample example) throws DataAccessException {
		return mapper.getPageByModel(pageSize, pageBegin, example); 
	}
	/**
	 * 查找条数
	 * 
	 * @param example
	 * @return
	 * @throws DataAccessException
	 */
	public int countByExample(MerchantInfoExample example) throws DataAccessException {
		return mapper.countByExample(example);
	}

	/**
	 * 
	 */
	public List<MerchantInfo> getListByExample(MerchantInfoExample example)
			throws DataAccessException {
		return mapper.selectByExample(example); 
	}

	public MerchantInfo selectByPrimaryKey(String key) throws DataAccessException {
		return mapper.selectByPrimaryKey(key);
	}
	/**
	 * 新增商户终端关联表
	 * 
	 * @param merchantId
	 * @param terminalId
	 * @throws DataAccessException
	 */
	public void addMerchantTerminalInfo(String merchantId, String terminalId)
			throws DataAccessException {
		MerchantTerminalInfo record = new MerchantTerminalInfo();
		record.setMerchantId(merchantId);
		record.setTerminalId(terminalId);
		record.setCreateDate(new Date());
		merchantTerminalInfoMapper.insert(record);
	}
	public void deleteMerchantTerminalInfo(String merchantId)
			throws DataAccessException {
		MerchantTerminalInfoExample example = new MerchantTerminalInfoExample();
		example.createCriteria().andMerchantIdEqualTo(merchantId);
		merchantTerminalInfoMapper.deleteByExample(example);
	}
	public void deleteMerchantTerminalInfo(String merchantId, String terminalId)
			throws DataAccessException {
		MerchantTerminalInfoExample example = new MerchantTerminalInfoExample();
		example.createCriteria().andMerchantIdEqualTo(merchantId);
		example.createCriteria().andTerminalIdEqualTo(terminalId);
		merchantTerminalInfoMapper.deleteByExample(example);
	}
	/**
	 * 新增商户证件信息关联表
	 * 
	 * @param record
	 * @throws DataAccessException
	 */
	public void addMerchantCertificateInfo(MerchantCertificateInfo record)
			throws DataAccessException {
		
		record.setCreateDate(new Date());
		merchantCertificateInfoMapper.insert(record);
		
	}
	public void updateMerchantCertificateInfo(MerchantCertificateInfo record)
			throws DataAccessException {
		
		record.setUpdateDate(new Date());
		merchantCertificateInfoMapper.updateByPrimaryKey(record);
	}
	public MerchantCertificateInfo getMerchantCertificateInfo(String merchantId)
			throws DataAccessException {
		return merchantCertificateInfoMapper.selectByPrimaryKey(merchantId);
	}
	/**
	 * 根据商户获得绑定的终端关联表
	 */
	public List<MerchantTerminalInfo> getMerchantTerminalInfoByMerchantId(
			String merchantId) throws DataAccessException {
		MerchantTerminalInfoExample example = new MerchantTerminalInfoExample();
		example.createCriteria().andMerchantIdEqualTo(merchantId);
		return merchantTerminalInfoMapper.selectByExample(example);
	}

	@Override
	public int updateBelong(String fromAgentId, String toAgentId) {
		return mapper.updateBelong(fromAgentId, toAgentId);
	}

	@Override
	public List<String> getMerchantIdList(MerchantInfoExample example) {
		return mapper.getMerchantIdList(example);
	}
}
